package com.swift.violent.mainstay.config.expand;

import cn.hutool.core.annotation.AnnotationUtil;
import com.github.alenfive.rocketapi.entity.ApiInfo;
import com.github.alenfive.rocketapi.extend.ApiInfoInterceptor;
import com.swift.violent.mainstay.common.constants.Global;
import com.swift.violent.mainstay.config.CoreConfig;
import com.swift.violent.mainstay.config.annotation.EnableRySecurityFunctionConfig;
import com.swift.violent.mainstay.security.interceptor.PreAuthorizeInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 *
 * @author xuhaifeng
 * @date 2021.1.19 23:17
 */
public class DefaultApiInfoInterceptor extends ApiInfoInterceptor {
    private static final String ACTIVATION_KEY = "activation";

    /**
     * 诺依安全方法状态
     * Ry security function string.
     *
     * @return the string
     */
    public static String rySecurityFunctionStatus() {
        return AnnotationUtil.getAnnotationValue(CoreConfig.class, EnableRySecurityFunctionConfig.class, ACTIVATION_KEY);
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ApiInfo apiInfo) {
        return !Global.NUM_1.equals(rySecurityFunctionStatus()) || PreAuthorizeInterceptor.annotationInterceptor(apiInfo.getOptions());
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView, ApiInfo apiInfo) {
    }

}